/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package main;

//import Scenery.BaseCharacter;
//import Scenery.BaseSprite;
//import Scenery.DumbMath;
import Scenery.BaseCharacter;
import com.jme3.input.ChaseCamera;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.CameraNode;
import com.jme3.scene.Node;
import com.jme3.scene.control.CameraControl.ControlDirection;
import java.awt.Point;

/**
 *
 * @author Ziden
 */

public class RoundCamera {
    
    private float camX =0 , camY=0;
    private float angle;
    private final float camSpeed = 0.1111101f;
    private final CameraNode camNode;
    private final Node camMasterNode;
    private final int camDistance = 50;
    private float angleModifier = 0f;
    private BaseCharacter target;
    
    public float getAngle() {
        return angle;
    }
    
    public CameraNode getNode() {
        return camNode;
    }
    
    public RoundCamera(Camera cam, BaseCharacter dude) {
        camMasterNode = new Node("CamMasterNode");
        camNode = new CameraNode("CamNode", cam);
        camNode.setControlDir(ControlDirection.SpatialToCamera);
        camNode.setLocalTranslation(new Vector3f(camDistance/3, camDistance/2, camDistance/3));
        camMasterNode.attachChild(camNode);
        dude.getMasterNode().attachChild(camMasterNode);
        camNode.lookAt(dude.getControl().getPhysicsLocation(), Vector3f.UNIT_Y);
       // camNode.lookAt(dude.getModel().getLocalTranslation(), Vector3f.UNIT_Y);
        target = dude;
        changeAngle();
    }   
    
    private float angleAceleration = 0;
    private final float maxSpeed = 5.05f;
    
    public void acelerate(int dir) {
        if(dir==9 && angleAceleration < maxSpeed) {
            angleAceleration+=camSpeed;
        }
        else if(dir==7 && angleAceleration > -maxSpeed)
            angleAceleration-=camSpeed;
        changeAngle();
    }
    
    public void deacelerate() {
        if(angleAceleration!=0) {
            if(angleAceleration > 0) { 
                angleAceleration -= camSpeed;
                if(angleAceleration<0) angleAceleration = 0; 
            }
            else if(angleAceleration < 0)  {
                angleAceleration+=camSpeed;
                if(angleAceleration > 0) angleAceleration = 0;
            }
            changeAngle();
        }
    }
    
    public void volta() {
    }
    
  
    private float perimetro = 0;

    private boolean locked = false;
    
    public void changeAngle() {
        angleModifier += angleAceleration * Main.globalTpf; 
        DumbMath.turnTo(camMasterNode, angleModifier);
        angle = sumAngle(DumbMath.getNodeAngle(camMasterNode),45);//DumbMath.getAngle(FastMath.sin(angleModifier) * camDistance, FastMath.cos(angleModifier) * camDistance);
    }
    
    public float sumAngle(float angle1, float angle2) {
        if(angle1+angle2 > 360)
            return (angle1+angle2)%360;
        return (angle1+angle2);
    }
}
